<?php
namespace application\admin\panels;

use application\utilities\Utility;
use application\models\ContentType;
class get_modules extends panel {
	public $name = 'Welcome to Concerto';
	
	public function sidebar() {
		$stubs = DB()->getCount("SELECT * FROM `cn_content` WHERE `content_status`='stub'");
		$stubTypes = DB()->getResults("SELECT `ct_plural`, `ct_id`, COUNT(`content_id`) AS `count` FROM `cn_content` INNER JOIN `cn_content_types` ON `content_type`=`ct_id` WHERE `content_status`='stub' GROUP BY (`content_type`)");
		if($stubs) {
			$stubTypeText = array();
			foreach($stubTypes as $stub) {
				$stubTypeText[] = '<a href="javascript:concerto.admin.loadPanel(\'manage\', {type:'.$stub->ct_id.',status:\'stub\'});">' .$stub->count . ' '. $stub->ct_plural.'</a>';
			}
		
		}
		else {
			$stubs = '0';
		}
		
		$pendings = DB()->getCount("SELECT * FROM `cn_content` WHERE `content_status`='pending'");
		$pendingTypes = DB()->getResults("SELECT `ct_plural`, `ct_id`, COUNT(`content_id`) AS `count` FROM `cn_content` INNER JOIN `cn_content_types` ON `content_type`=`ct_id` WHERE `content_status`='pending' GROUP BY (`content_type`)");
		if($pendings) {
			$pendingTypeText = array();
			foreach($pendingTypes as $pending) {
				$pendingTypeText[] = '<a href="javascript:concerto.admin.loadPanel(\'manage\', {type:'.$pending->ct_id.',status:\'pending\'});">' .$pending->count . ' '. $pending->ct_plural.'</a>';
			}
		
		}
		else {
			$pendings = '0';
		}
		
		$totalCount = DB()->getCount("SELECT `content_id` FROM `cn_content`");
		$totalUsers = DB()->getCount("SELECT `user_id` FROM `cn_users`");
		?>
		<div class="inner">
			<h3>Site Statistics</h3>
			<ul class="sidebar-list">
				<li>
					You have <b><?=$pendings?></b> pending contents. 
					<?php if($pendings) : ?>
					<ul>
					<?php foreach($pendingTypeText as $pending) : ?>
						<li><?=$pending?></li>
					<?php endforeach;?>
					</ul>
					<?php endif;?>
				</li>
				<li>
					You have <b><?=$stubs?></b> stubs to fill out.
					<?php if($stubs) : ?>
					<ul>
					<?php foreach($stubTypeText as $stub) : ?>
						<li><?=$stub?></li>
					<?php endforeach;?>
					</ul>
					<?php endif;?>
				</li>
				<li>
					There are <b><?=$totalCount?></b> pieces of content on your site.
				</li>
				<li>
					There are <b><?=$totalUsers?></b> users on your site.
				</li>
			</ul>
		</div>
		<?php
	}
	
	public function newContentType() {
		$contentType = new ContentType;
		$contentType->create(array('title'=>$_REQUEST['singular'], 'plural'=>$_REQUEST['plural']));
		$messages = $contentType->add();
		$this->mergeMessages($messages);
		
		echo json_encode(array('messages'=>$this->messages, 'id'=>$contentType->getId()));
	}
	
	public function handleData() {
		global $concerto;

		if($_SESSION['fresh_install']) {
			$format = $_SESSION['site_format'];
			if($format) {
				require CN_APPLICATION_PATH . 'formats/'.$format.'/install.php';
				setOption('site_format', $format);
				$this->addMessage('success', ucwords($format).' format installed.');
			}
			unset($_SESSION['fresh_install']);
		}
		if($_REQUEST['clear_errors']) {
			DB()->query("DELETE FROM `cn_pageviews` WHERE `pv_type`='error'");
			$this->successes[] = 'Errors cleared successfully.';
		}
		
		elseif($_REQUEST['send_feedback']) {
			/** For now, just send us feedback via e-mail */
			$from = $concerto->User->email;
			$message = $_REQUEST['feedback'];
			mail('jason@limitdesigns.com', 'Concerto Feedback', $message, 'From: '.$from);
			$messages['success'] = "Thanks for sending us some feedback. We'll get back to you soon.";
			$this->mergeMessages($messages);
		}
		
		// Check for updates
		/**if($contents = self::checkForUpdates()) {
			$this->alerts[] = 'A new version of Concerto ('.$contents->version.') is available. Click <a href="'.CN_URL.'admin/?display=update">here</a> to install';
			$this->alerts[] = '<b>Version Notes: </b><br />'.$contents->notes;
		
		}*/
		
		
	}
	
	protected static function checkForUpdates() {
		global $concerto;
		$contents = file_get_contents('http://www.limitdesigns.com/concerto/latest_version/version.php');
		$contents = json_decode($contents);
		$version = $contents->version;
		$currentVersion = $concerto->getOption('current_version');
		if($version > $currentVersion) return $contents;
		else return FALSE;
	}
	
	public function title() {
		return CN_SITE_NAME . ' Administration';
	}
	
	
	public function display() {
		global $concerto;
		
		Modulator()->runFunction('admin_home');?>
		<div class="box">
			<h3>Welcome back.</h3>
			<div class="box-content">
				<div class="two-column-left">
				<h4>What's new in version 2?</h4>
				Version 2 of Concerto is so awesome, it even blows <i>us</i> away! Here's some stuff to look out for in version 2.
				<ul class="box-list">
				<li>
					<h5>New Administration Panel</h5>
					As you can see, Concerto is boasting an entirely new administration panel. The old panel had a steep learning curve and wasn't so aesthetically pleasing; this new one is both intuitive and beautiful, making use of some slick JavaScript techniques to make your experience as easy as possible.
				</li>
				<li>
					<h5>Geek Mode</h5>
					See the wrench icon at the top right of the screen? That's the "geek mode" switch. If you're in geek mode, you have full control over your Concerto installation, from content types to modules to code generation. Turn it off, and you'll only see the stuff you need in order to update and manage your site's content.
				</li>
				<li class="geek-mode">
					<h5>Completely Custom Forms</h5>
					We totally redid Concerto's form rendering engine. Now you have complete control over the look and feel of your forms, literally down to the last radio button - forms can be automatically generated by the engine or sent to one of your own templates, where you can use simple PHP functions within the HTML to display the form fields.
				</li>
				<li class="geek-mode">
					<h5>New Data Type API</h5>
					Data types are now simpler than ever with the new API design - we've consolidated all data types into one file each, so all methods and properties for each type can be found in one single location. That means that coding your own data types no longer involves any hooks and filters; simply add your data type file to the system and you're good to go.
				</li>
				</ul>
				</div>
				<div class="two-column-right">
					<h4>Other stuff</h4>
					asdfasdfasdf
				</div>
			</div>
		</div>
		
		
		<?php
	}
}
?>